// pages/duobao/detail/detail.js
import S_request from '../../../utils/requestService.js';
import CONFIG from '../../../config.js';
import * as utils from '../../../utils/util.js';
const app = getApp()
let detailId = 0 //详情页的id
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowQuan: false,
    isShowZj: false,
    modalObj: {},
    iconList: [],
    detailInfo: null,
    zjInfo: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    detailId = options.id;
    this.getDetailInfo(detailId);
  },
  getDetailInfo: function(id) {
    S_request.duobao.getDBDetail(id, (result) => {
      result.introduction = utils.formatStr(result.introduction);
      result.game_poster = utils.formatStr(result.game_poster);
      result.kj_time = utils.formatTime(result.open_time, 2);
      if (result.status == 0) {
        if (result.userinfo.count == 0) { //未参与抽奖，显示立即参与
          result.joinTitle = '参与抽奖'
          result.joinStyle = 'background:linear-gradient(#d05042, #dc4131);box-shadow: 0px 5rpx 8rpx #fba9a1;text-shadow: 0px 3rpx 1rpx #b22719;'
        } else {
          result.btnTitle = '等待开奖'
          result.btnStyle = 'border:0; background-color:#e42c2c;'
          result.joinTitle = '再次抽奖'
          result.joinStyle = 'background:linear-gradient(#f28145, #ed6e21);box-shadow: 0px 5rpx 8rpx #fea56f;text-shadow: 0px 3rpx 1rpx #c35615;'
        }
      } else {
        result.btnTitle = '已开奖'
        result.btnStyle = 'border:0; background-color:#9a9898;'
        result.joinTitle = '已开奖'
        result.joinStyle = 'background:linear-gradient(#b8b4b4,#9a9898);box-shadow: 0px 5rpx 8rpx #a8a7a7;text-shadow: 0px 3rpx 1rpx #717070;'
      }
      if (result.status == 1 && result.userinfo.count != 0) {
        let now_time = new Date().getTime();
        if (now_time - result.open_time*1000 < 86400000 * 3) { //超过三天，不播放动画
          let tempStr = wx.getStorageSync('kjIds');
          if (tempStr != '') {
            let idArr = tempStr.split(",");
            let tempId = -1
            for (let i = 0; i < idArr.length; i++) {
              if (idArr[i] == result.id) { //已经播放过动画
                tempId = idArr[i]
                break;
              }
            }
            if (tempId == -1) {
              this.showKjAni(result.me, result.win); //显示开奖动画
              wx.setStorageSync('kjIds', tempStr + result.id + ",")
            }
          } else {
            this.showKjAni(result.me, result.win); //显示开奖动画
            wx.setStorageSync('kjIds', tempStr + result.id + ",")
          }
        }
        if (result.me == 1) {
          result.btnTitle = '领取奖品'
          result.btnStyle = 'border:0; background-color:#e42c2c;'
        }
      }
      if (parseInt(result.jindu) > 0) {//领取奖品之后 "领取奖品"按钮隐藏
        result.btnTitle = '';
        result.btnStyle = 'display:none'
      }
      this.setData({
        detailInfo: result
      })
      if (result.status == 1) {
        this.kjUI(result);
      }

    })
  },
  //参与抽奖
  patiCJ: function() {
    let info = this.data.detailInfo;
    if (info.status == 1) return;
    let id = info.id;
    if (parseInt(info.money) > parseInt(info.userinfo.tickets)) {
      wx.showToast({
        title: '抽奖券不足',
        icon: 'none',
        duration: 2000
      })
    } else {
      S_request.duobao.joinDB(id, (result) => {
        wx.showModal({
          title: '提示',
          content: '参与成功',
          showCancel: false
        })
        this.getDetailInfo(detailId);
      })
    }
  },
  //开奖界面显示
  kjUI: function(result) {
    let zjInfo = {}
    if (result.userinfo.count == 0) { //没有参与抽奖
      zjInfo.icon = '../image/zj_icon.png'
      zjInfo.title = '恭喜以下小伙伴们'
      zjInfo.titleStyle = 'color:#d1594e'
    } else {
      if (result.me) { //中奖
        zjInfo.icon = '../image/zj_icon.png'
        zjInfo.title = '恭喜，您已中奖'
        zjInfo.titleStyle = 'color:#d1594e'
      } else {
        zjInfo.icon = '../image/unzj_icon.png'
        zjInfo.title = '差一点点就中奖了，继续努力！'
        zjInfo.titleStyle = 'color:#333'
      }
    }
    if (result.win.length == 1) {
      zjInfo.bodyStyle = 'width:154rpx;'
    } else if (result.win.length == 2) {
      zjInfo.bodyStyle = 'width:308rpx;'
    } else {
      zjInfo.bodyStyle = 'width:462rpx;'
    }
    this.setData({
      zjInfo: zjInfo
    })
  },
  getReward: function(e) {
    let id = e.currentTarget.dataset.id;
    let detailInfo = this.data.detailInfo
    if (detailInfo.status == 1 && detailInfo.userinfo.count != 0 && detailInfo.me == 1) {
      wx.navigateTo({
        url: '../../shopExcAddr/shopExcAddr?goodId=' + detailInfo.ids + "&type=" + detailInfo.type,
      })
    }
  },
  showKjAni: function(isZj, winList) {
    setTimeout(function() {
      this.setData({
        isShowkj: true
      })
    }.bind(this), 500)
    setTimeout(function() {
      this.data.modalObj.isZj = isZj;
      this.data.modalObj.winList = winList
      this.setData({
        isShowkj: false,
        isShowZj: true,
        modalObj: this.data.modalObj
      })
    }.bind(this), 3000)
  },
  allUsers: function() {
    wx.navigateTo({
      url: '../partiUsers/partiUsers?id=' + this.data.detailInfo.id + "&num=" + this.data.detailInfo.info.player_num,
    })
  },
  excQuan: function() {
    this.setData({
      isShowQuan: true
    })
  },
  closeBtnAc: function() {
    //重新请求一下数据，兑换成功，显示最新的奖券
    this.getDetailInfo(detailId);
    this.setData({
      isShowQuan: false
    })
  },
  closeZjBtnAc: function() {
    this.setData({
      isShowZj: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getDetailInfo(detailId);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    wx.showShareMenu({
      withShareTicket: true,
      success: function () { }
    });
    let obj = { 'pagepath': CONFIG.SHARE.pathDuobao}
    S_request.index.recordShare("-6", obj);
    let name = app.globalData.userInfo.nickName;
    let title = name + "邀请你参与【" + this.data.detailInfo.game_name+"】抽奖"
    let path = CONFIG.SHARE.pathDuobao + "&dbId=" + this.data.detailInfo.id
    console.log("path=="+path)
    return {
      title: title,
      path: path,
      imageUrl: '',
      success: res => { //新版不走回调
        console.log("分享成功")
      }
    }
  }
})